package com.huang.service.crud;

import com.huang.bo.UserInfo;
import com.huang.service.impl.ICrudService;
import com.huang.service.impl.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 增删改查的实现类
 */

//@Transactional //开启事务
@Service
public class ICrudServiceImpl  {



//	@Qualifier("emailServiceImpl")
//	@Resource
//	private IEmailService emailService;

	/**
	 * 注册
	 * @param userInfo
	 */

//	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Autowired
	private JdbcTemplate jdbcTemplate;
	public void add(UserInfo userInfo) {
		System.out.println(111);
		String sql = "insert into userinfo values (?,?,?,?)";
		jdbcTemplate.update(sql, null, userInfo.getPhone(), userInfo.getPassword(), userInfo.getSex());

//		emailService.addEmailInfo();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}


	public void delete(String id) {

		String sql = "delete from userinfo where id = ?";
		jdbcTemplate.update(sql, id);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}


	public void update(String id) {

		String sql = "update from userinfo sex = ? where id = ?";
		jdbcTemplate.update(sql, "男", id);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}


	public void querySomeoneInfo(String id) {

		String sql = "select * from userinfo where id = ?";
		RowMapper<UserInfo> rowMapper = new BeanPropertyRowMapper<UserInfo>(UserInfo.class);
		UserInfo userInfo = jdbcTemplate.queryForObject(sql, rowMapper, id);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}


	public List<UserInfo> queryAllInfo() {
		String sql = "select * from userinfo";
		RowMapper<UserInfo> userInfoRowMapper = new BeanPropertyRowMapper<UserInfo>(UserInfo.class);
		List<UserInfo> userInfoList = jdbcTemplate.query(sql, userInfoRowMapper);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return userInfoList;
	}




}
